<?php
/**
 * Created by IntelliJ IDEA.
 * User: jimmyhsu
 * Date: 2017/4/24
 * Time: 21:56
 */
include ("../Teacher/db/conn.php");
$username = $_POST['username'];
$password = $_POST['password'];
$course_id = $_POST['course_id'];
$sql = "select * from userinfo natural join takes where name='$username' and password='".md5($password)."' and course_id='$course_id'";

$result = mysqli_query($cn, $sql);
$resultArr = array();
if (mysqli_num_rows($result) > 0) {
    $sql = "select * from bulletin join userinfo where course_id='$course_id' and author_id=id order by b_id desc";
    $query = mysqli_query($cn, $sql);
    if ($query) {
        $resultArr['code'] = 200;
        $jsonArr = array();
        while ($line = mysqli_fetch_assoc($query)) {
            array_push($jsonArr, array('course_id'=>$line['course_id'], 'b_id'=>$line['b_id'], 'title'=>$line['title'], 'content'=>$line['content'],
                'author'=>$line['real_name'], 'release_time'=>$line['release_time'], 'image_url'=>$line['image_url']));
        }
        $resultArr['data'] = $jsonArr;
    } else {
        $resultArr['code'] = 500;
        $resultArr['error_message'] = 'database query failed';
    }
} else {
    $resultArr['code'] = 501;
    $resultArr['error_message'] = 'permission denied';
}
echo json_encode($resultArr);